package com.quanshu.exchange.ui.gui.message.group;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.UserInterface;
import com.quanshu.exchange.support.entity.GroupEntity;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SendGroupApplyActivity extends BaseActivity {

    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.send_group_validate_input_text)
    EditText inputText;
    @BindView(R.id.send_group_validate_btn)
    Button sendBtn;
    private GroupEntity groupEntity;
    private String strMessage;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_group_apply);
        ButterKnife.bind(this);
        initIntent();
        initView();
        initListener();
    }

    private void initIntent() {
        groupEntity = (GroupEntity) getIntent().getSerializableExtra("GroupEntity");
    }

    private void initView() {
        String name = AppUtils.getUserName(currentUser);
        if (!TextUtils.isEmpty(name)) {
            inputText.append(name);
        }
    }

    private void initListener() {
        RxViewHelper.getInstance().clicks(back, aVoid -> finish());
        RxViewHelper.getInstance().clicks(sendBtn, aVoid -> doSendApply());
        RxViewHelper.getInstance().afterTextChangeEvents(inputText,
                textViewAfterTextChangeEvent -> {
                    strMessage = textViewAfterTextChangeEvent.editable().toString();
                    inputText.setSelection(strMessage.length());
                });
    }

    private void doSendApply() {
        if (TextUtils.isEmpty(strMessage)) {
            strMessage = "请求加入群聊";
        }
        Map<String, String> map = new HashMap<>();
        map.put("apply_content", strMessage);
        map.put("hx_groupid", groupEntity.getHx_groupid());
        NetClient.getInstance()
                .getNetService(UserInterface.class)
                .applyAddFriendOrGroup(AppUtils.getSignParams(groupEntity.getOwner(), currentUser.getMemberid(),
                        "2"), groupEntity.getOwner(), currentUser.getMemberid(), "2", map)
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse<String>>() {
                    @Override
                    public void onSuccess(BaseResponse<String> response) {
                        EMClient.getInstance().groupManager().asyncApplyJoinToGroup(groupEntity.getHx_groupid(), strMessage, new EMCallBack() {
                            @Override
                            public void onSuccess() {
                                runOnUiThread(() -> {
                                    ToastHelper.toast("发送成功，等待群主验证");
                                    finish();
                                });
                            }

                            @Override
                            public void onError(int code, String error) {
                                runOnUiThread(() -> {
                                    ToastHelper.toast("发送失败，error=" + error);
                                    finish();
                                });
                            }

                            @Override
                            public void onProgress(int progress, String status) {

                            }
                        });
                    }
                });
    }

}
